// pages/pay/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topicData:{},
    money:1,
    moneyList:[0.1,1,5,10,15,20,30,35,50],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.topic_id = options.topic_id ? options.topic_id : 83527;
    this.getTopic();
    //this.pay();
  },

  getTopic(){
    var that = this;
    app.api('topic/detail', {
      id: that.data.topic_id
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return false;
      }
      that.setData({ topicData:res.data});
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  pay:function(e){
    var that =this;
    this.data.money = e.currentTarget.dataset.money;
    app.api("pay/topic_pay", {
      order_amount: this.data.money,
      title:"为计划加油",
      body: this.data.topicData.title,
      topic_id: this.data.topic_id,
    },function(res){
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      var order_sn = res.data.order_sn;
      app.api("pay/topay", {
        order_sn: order_sn
      }, function (res) {
        if (res.code != 200) {
          app.alert(res.msg);
          that.data.payloading = 0;
          that.setData({ 'loading': 0 });
          that.setData({ 'disabled': 0 });
          return false;
        }
        
        var params = res.data;
        //params.total_fee = 0.1;
        params.success = function (res) {
          app.setPageData("topic/detail", { reload:1});
          wx.navigateBack({
            delta: 1
          })
        }
        params.fail = function (res) {
          console.log(res);
          if (res.errMsg == 'requestPayment:fail cancel') {
            //  wx.navigateBack({
            //    delta: 1
            //  })
            that.setData({ 'loading': 0 });
            that.setData({ 'disabled': 0 });
            return false;
          }
          app.alert(res.errMsg);
        }
        wx.requestPayment(params)
      })
      
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})